﻿using Microsoft.Xna.Framework.Input;
using AngryBullets.Input;
using AngryBullets.Sprite;
using Microsoft.Xna.Framework;

namespace AngryBullets.Sprite.Object
{
    class Boulet : AnimationSprite
    {
        #region Fields

        private Vector2 _bouletPosition;
        private Vector2 _bouletCenter;
        private Vector2 _bouletSpeed;
        private int time;
        private KeyboardState keyboard;
        private MouseState mouse;

        #endregion

        #region Initialisation

        public Boulet(Vector2 position)
            : base(new AnimationDefinition()
            {AssetName = "boulet", FrameRate = 8, FrameSize = new Point(15, 15),
             Loop = true, NbFrames = new Point(8, 1)})
        {
            _bouletPosition = position;
        }

        #endregion

        #region Move

        public void Move(GameTime gameTime)
        {
            base.Update(gameTime);
            time = gameTime.ElapsedGameTime.Milliseconds;
            keyboard = InputState.CurrentKeyboardState;
            mouse = InputState.CurrentMouseState;

            if (mouse.LeftButton == ButtonState.Pressed)
            {
                _bouletPosition = new Vector2(mouse.X, mouse.Y);
                SetSpeed();
            }
            else
                _bouletPosition = _bouletPosition + _bouletSpeed;
        }

        #endregion

        #region Methods

        public void SetSpeed()
        {
            CalcCenter();
            _bouletSpeed = Baliste.BalisteCenter - _bouletCenter;
        }

        private void CalcCenter()
        {
            _bouletCenter = _bouletPosition - new Vector2(15/2, 15/2);
        }

        #endregion
    }
}
